package com.yyj.util;

import java.io.UnsupportedEncodingException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 请求校验工具类
 * 
 * @author zhongwen ho
 */
public class SignUtil {
	private static Logger log = LoggerFactory.getLogger(SignUtil.class);
	/*
	// 与接口配置信息中的Token要一致
	private static String token = Code.CodeParam.WEIXIN_TOKEN;

	*//**
	 * 验证签名
	 * 
	 * @param signature
	 * @param timestamp
	 * @param nonce
	 * @return
	 *//*
	public static boolean checkSignature(String signature, String timestamp, String nonce) {
		String[] arr = new String[] { token, timestamp, nonce };
		// 将token、timestamp、nonce三个参数进行字典序排序
		Arrays.sort(arr);
		StringBuilder content = new StringBuilder();
		for (int i = 0; i < arr.length; i++) {
			content.append(arr[i]);
		}
		MessageDigest md = null;
		String tmpStr = null;

		try {
			md = MessageDigest.getInstance("SHA-1");
			// 将三个参数字符串拼接成一个字符串进行sha1加密
			byte[] digest = md.digest(content.toString().getBytes());
			tmpStr = byteToStr(digest);
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		}
		content = null;
		// 将sha1加密后的字符串可与signature对比，标识该请求来源于微信
		return tmpStr != null ? tmpStr.equals(signature.toUpperCase()) : false;
	}*/
	
	/**
	 * 生成签名（用于分享接口）
	 * 
	 * @param shareConfig
	 * @param url
	 * 
	 * @return
	 *//*
	public static String createSign(String jsapi_ticket, ShareConfig shareConfig, String url){
		
		String tmpStr = null;
		MessageDigest md = null;
		StringBuffer param = new StringBuffer("jsapi_ticket=");
		param.append(jsapi_ticket);
		param.append("&noncestr=");
		param.append(shareConfig.getNonceStr());
		param.append("&timestamp=");
		param.append(shareConfig.getTimestamp());
		param.append("&url=");
		param.append(url);
		try {
			md = MessageDigest.getInstance("SHA-1");
			byte[] digest = md.digest(param.toString().getBytes());
			tmpStr = byteToStr(digest);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return tmpStr;
	}*/

	/**
	 * 将字节数组转换为十六进制字符串
	 * 
	 * @param byteArray
	 * @return
	 */
	private static String byteToStr(byte[] byteArray) {
		String strDigest = "";
		for (int i = 0; i < byteArray.length; i++) {
			strDigest += byteToHexStr(byteArray[i]);
		}
		return strDigest;
	}

	/**
	 * 将字节转换为十六进制字符串
	 * 
	 * @param mByte
	 * @return
	 */
	private static String byteToHexStr(byte mByte) {
		char[] Digit = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
		char[] tempArr = new char[2];
		tempArr[0] = Digit[(mByte >>> 4) & 0X0F];
		tempArr[1] = Digit[mByte & 0X0F];

		String s = new String(tempArr);
		return s;
	}
	
	/**
	 * 计算采用utf-8编码方式时字符串所占字节数
	 * 
	 * @param content
	 * @return
	 */
	public static int getByteSize(String content) {
		int size = 0;
		if (null != content) {
			try {
				// 汉字采用utf-8编码时占3个字节
				size = content.getBytes("utf-8").length;
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
		}
		return size;
	}
	
	
	/**
	 * 微信分享签名 
	 * @param signParam
	 * 			signParam = {wid="",memberUnitNo="",eventtype="",signURL=""}
	 * 			eventtype=>活动类型(共6个字符：XXXXX_)，signURL=>当前需要分享的页面url
	 * @param request
	 * @return
	 * @throws HTTPServiceException
	 * @throws IOException 
	 *//*
	public static Map<String, Object> wechatShareSignajax(Map<String,Object> signParam,WeChatInfo wachat){
		Map<String, Object> signMap = new HashMap<String, Object>();
		try{
			String wid = signParam.get("wid").toString();
			String state = signParam.get("memberUnitNo").toString();
			String eventType = signParam.get("eventtype").toString(); //活动类型
			String current_url = signParam.get("signURL").toString(); //分享页面的url
			String share_url = WebConfig.get("share_url",state) + eventType +state+"||"+ wid + "#wechat_redirect";
			
			Map<String,Object> param  = new HashMap<String,Object>();
			param.put("appId", wachat.getAppid());
			param.put("memberUnit",state);	
			param.put("url", current_url);
			
			String result = HTTP.POST(WebConfig.get("apiRoot")+ "api/wechat/getShareParam.do", param);
			JSONObject result_json = JSONObject.parseObject(result);
			if(result_json.getBooleanValue("status")){
				JSONObject sign = JSONObject.parseObject(result_json.getString("result"));
				signMap.put("appId", sign.getString("appId"));
				signMap.put("timestamp", sign.getString("timestamp"));
				signMap.put("nonceStr", sign.getString("nonceStr"));
				signMap.put("signature", sign.getString("signature"));
				signMap.put("shareurl", share_url);
				signMap.put("pic_url", share_url);
				signMap.put("title", share_url);
				signMap.put("contents", share_url);
				
				if("AGENT_".equals(eventType)){
					signMap.put("pic_url",WebConfig.get("webRoot")+"static/images/newyear.jpg");
					signMap.put("contents",WebConfig.get("share_coupon_contents_agent"));
					signMap.put("title",WebConfig.get("share_coupon_title_agent"));
				}else{
					signMap.put("pic_url",WebConfig.get("webRoot")+"static/images/newyear.jpg");
					signMap.put("contents",WebConfig.get("share_coupon_contents"));
					signMap.put("title",WebConfig.get("share_coupon_title"));
				}
				
			}
		}catch(Exception e){ log.info(e.getMessage(), e); }
		return signMap;
	}*/
}

